﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.info.user
{
    public partial class query : ModulePage
    {
        protected string strTemp;
        public string DisplayMobile(string mobile, bool isHideMobile)
        {
            if (isHideMobile)
            {
                for (int i = 0; i <= 9; i++)
                {
                    mobile = mobile.Replace(i.ToString(), "*");
                }
                mobile = "<span title=\"不公开\" style=\"cursor:hand\">" + mobile + "</span>";
            }
            return mobile;
        }
        private void LoadDeptTree(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.LoadDeptTree(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item = new ListItem("所有", "ALL");
                this.ddlDeptId.Items.Add(item);
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.LoadDeptTree(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                this.litNavName.Text = "人员查询结果";
                this.pnlMain.Visible = false;
                SqlCommand command = new SqlCommand();
                base.Conn.Open();
                command.Connection = base.Conn;
                string str = "SELECT [Users].*,[Department].[DeptName],[User_Priv].[PrivName] FROM [dbo].[Users],[dbo].[Department],[dbo].[User_Priv] ";
                str = str + " WHERE [Users].[DeptId] <> -1 AND [Users].[DeptId] = [Department].[DeptId] AND [Users].[PrivId] = [User_Priv].[PrivId] ";
                if (this.txtUserName.Text != string.Empty)
                {
                    str = str + " AND [Users].[UserName] LIKE '%' + @UserName + '%' ";
                    command.Parameters.Add("@UserName", this.txtUserName.Text);
                }
                if (this.ddlDeptId.SelectedValue != "ALL")
                {
                    str = str + " AND [Department].[DeptId] = @DeptId ";
                    command.Parameters.Add("@DeptId", Convert.ToInt32(this.ddlDeptId.SelectedValue));
                }
                if (this.ddlSex.SelectedValue != "ALL")
                {
                    str = str + " AND [Users].[Sex] = @Sex ";
                    command.Parameters.Add("@Sex", Convert.ToBoolean(this.ddlSex.SelectedValue));
                }
                if (this.txtTelNoDept.Text != string.Empty)
                {
                    str = str + " AND [Users].[TelNoDept] = @TelNoDept ";
                    command.Parameters.Add("@TelNoDept", this.txtTelNoDept.Text);
                }
                if (this.txtEmail.Text != string.Empty)
                {
                    str = str + " AND [Users].[Email] = @Email ";
                    command.Parameters.Add("@Email", this.txtEmail.Text);
                }
                str = str + " ORDER BY [Department].[DeptNo] ASC, [Users].[UserNo] ASC";
                command.CommandText = str;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    this.rptUsers.DataSource = reader;
                    this.rptUsers.DataBind();
                    reader.Close();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("无符合条件的人员", "query.aspx");
                }
            }
        }

    }
}
